/*
 =============================================================================
 FILE...............: GenericAO.java
 COMMENTS...........: Generic abstract class implemented by using the DAO 
 					  (Data Access Object) design pattern for manipulating 
 					  model objects while accessing the database. The DAO
 					  classes which extend this class must implement the
 					  abstract methods.
 =============================================================================
 AUTHOR(S)..........: Everton Ranielly de Sousa Cavalcante
 					  Department of Informatics and Applied Mathematics
 					  Federal University of Rio Grande do Norte
 LAST MODIFICATION..: September 17th, 2012
 ============================================================================= 					  
*/


/* ========================================================================= */
/* PACKAGES			                                                         */
/* ------------------------------------------------------------------------- */
package dao;


/* ========================================================================= */
/* IMPORTS			                                                         */
/* ------------------------------------------------------------------------- */
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;


/* ========================================================================= */
/* CLASS GenericDAO                                                          */
/* ------------------------------------------------------------------------- */
/**
 * Generic abstract class implemented by using the DAO (Data Access Object) 
 * design pattern for manipulating  model objects while accessing the database. 
 * The DAO classes which extend this class must implement the abstract methods.
 * @param <T> Generic object type
 * @param <E> Generic key type
 * @author Everton Cavalcante
 */
public abstract class GenericDAO<T, E> 
{
	protected Connection conn = null;			// Connection object
	protected Statement stmt = null;			// Statement object database
	protected PreparedStatement pstmt = null;	// Prepared statement object database
	
	
	/** Opens a connection to the database */
	protected void openConnection() throws IOException
	{
		try 
		{
			// Creates a new connection
			conn = ConnectionFactory.connection("database.properties");
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
		}
		catch (SQLException e) 
		{ 
			System.out.println("Error when opening the connection:\n" + e.getMessage());
			System.exit(1);
		}
	}
	
	
	/** Closes a connection to the database */
	protected void closeConnection()
	{
		try 
		{
			// Releases the Statement object database and JDBC resources
			if (stmt != null)
				stmt.close();
			
			// Releases the prepared Statement object database and JDBC resources
			if (pstmt != null)
				pstmt.close();
			
			// Closes the connection
			if (conn != null)
				conn.close();
		}
		catch (SQLException e) 
		{ System.out.println("Error when closing the connection:\n" + e.getMessage()); }
	}
	
	
	/**
	 * Inserts a generic object
	 * @param object Generic object to insert
	 */
	public abstract void insert(T object);
	
	
	/**
	 * Updates a generic object based on a generic key
	 * @param object Object to update
	 * @param key Object key
	 */
	public abstract void update(T object, E key);
	
	
	/**
	 * Deletes a generic object based on a generic key
	 * @param key Object key
	 */
	public abstract void delete(E key);
	
	
	/**
	 * Gets the generic object based on a generic key
	 * @param key Key
	 * @return Generic object specified by the generic key
	 */
	public abstract T get(E key);
	
	
	/**
	 * Returns a collection of all generic objects
	 * @return List of generic objects
	 */
	public abstract List<T> getAll();
}
